﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Tasx.ViewModels
{
	public class DelegatingCommand : ICommand
	{
		private readonly Action<object> _execute = null;
		private readonly Func<object, bool> _canExecute = null;

		public DelegatingCommand(Action<object> execute, Func<object, bool> canExecute)
		{
			if (execute == null)
			{
				throw new ArgumentNullException("execute");
			}

			_execute = execute;
			_canExecute = canExecute;
		}

		public bool CanExecute(object parameter)
		{
			return _canExecute != null ? _canExecute(parameter) : true;
		}

		public event EventHandler CanExecuteChanged
		{
			add { CommandManager.RequerySuggested += value; }
			remove { CommandManager.RequerySuggested -= value; }
		}

		public void Execute(object parameter)
		{
			_execute(parameter);
		}
	}
}
